package pl.edu.mimuw.irs.rcp.controller.handler;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import pl.edu.mimuw.irs.core.model.analysis.Report;
import pl.edu.mimuw.irs.core.model.analysis.workflow.AnalysisWorkflow;
import pl.edu.mimuw.irs.core.model.design.IRSWorkflow;
import pl.edu.mimuw.irs.rcp.controller.editor.AnalysisWorkflowEditor;
import pl.edu.mimuw.irs.rcp.controller.editor.IRSWorkflowEditor;
import pl.edu.mimuw.irs.rcp.controller.editor.ReportEditor;
import pl.edu.mimuw.irs.rcp.controller.editor.input.AnalysisWorkflowEditorInput;
import pl.edu.mimuw.irs.rcp.controller.editor.input.ReportEditorInput;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-26
 */
public class CreateAnalysisWorkflowHandler extends AbstractHandler {

	// *** Attributes

	// *** Methods

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IRSWorkflowEditor editor = (IRSWorkflowEditor) HandlerUtil.getActiveWorkbenchWindow(event)
				.getActivePage().getActiveEditor();
	
		IRSWorkflow workflow = editor.getModel();
		
		AnalysisWorkflow analysisWorkflow = workflow.getAnalysisWorkflow();
		AnalysisWorkflowEditorInput input = new AnalysisWorkflowEditorInput();
		input.setAnalysisWorkflow(analysisWorkflow);
		Report report = createReport(analysisWorkflow, event);
		ReportEditorInput reportInput = new ReportEditorInput(report);
		
		try {
			HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
					.openEditor(input, AnalysisWorkflowEditor.ID);
			HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
					.openEditor(reportInput, ReportEditor.ID);
		} catch (PartInitException e) {
			Logger.handle(MessageCode.ANALYSIS_CREATE_PROBLEM, e, AnalysisWorkflowEditor.ID);
			return null;
		}
		
		return null;
	}

	private Report createReport(final AnalysisWorkflow analysisWorkflow, ExecutionEvent event) {
		
		ProgressMonitorDialog dialog = new ProgressMonitorDialog(HandlerUtil.getActiveShell(event));
		final Report report = new Report(analysisWorkflow);
		try {
			dialog.run(true, true, new IRunnableWithProgress(){
			    public void run(IProgressMonitor monitor) {
			        monitor.beginTask("Some nice progress message here ...", 100);
			        report.generate(monitor, 10000);
			        monitor.done();
			    }
			});
		} catch (InvocationTargetException e) {
			// XXX Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// XXX Auto-generated catch block
			e.printStackTrace();
		}
//		XXX : generation as a job
//		final Job job = new Job("Generating report...") {
//		    @Override
//		    protected IStatus run(IProgressMonitor monitor) {
//		        monitor.beginTask("Some nice progress message here ...", 100);
//		        analysisWorkflow.createReport(monitor);
//		        monitor.done();
//		        return Status.OK_STATUS;
//		    }
//		};
//		
		
		return report;
	}

}
